package com.xy_pro.xymanager.controller;

import com.xy_pro.xymanager.auth.dto.LoginRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.xy_pro.xymanager.auth.dto.LoginResponse;
import com.xy_pro.xymanager.auth.dto.ResultResponse;
import com.xy_pro.xymanager.service.AuthService;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import static com.mysql.cj.conf.PropertyKey.logger;

@RestController
@RequestMapping("/api/auth")
@RequiredArgsConstructor
public class AuthController {

    private final AuthService authService;
    // 创建日志对象（建议使用类名作为日志名称）
    private static final Logger logger = LoggerFactory.getLogger(AuthController.class);

    @PostMapping("/login")
//    @PreAuthorize("hasRole('ADMIN')")
    public ResultResponse<?> login(@Valid @RequestBody LoginRequest loginRequest) {
        logger.info("loginRequest:{}", loginRequest);
        LoginResponse loginResponse = authService.login(loginRequest);
        logger.info("loginResponse: {} " , loginResponse);
        return ResultResponse.success(loginResponse);
    }

    /// //普通用户登录，要与微信关联。
    @PostMapping("/loginUser")
//    @PreAuthorize("hasAnyRole('USER','TEACHER')")
    public ResultResponse<LoginResponse> loginUser(@Valid @RequestBody LoginRequest loginRequest) {
        LoginResponse loginResponse = authService.loginUser(loginRequest);
        logger.info("loginUser_Response: {} " , loginResponse);
        return ResultResponse.success(loginResponse);
    }
}
